<?php 

require_once("database.php");

function dwfile($file) 
{ 
    if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT'])) 
    { 
        // IE Bug in download name workaround 
        ini_set( 'zlib.output_compression','Off' ); 
    } 
    header ('Content-type: ' . mime_content_type($file)); 
    header ('Content-Disposition: attachment; filename="'.basename($file).'"'); 
    header ('Expires: '.gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y"))).' GMT'); 
    header ('Accept-Ranges: bytes'); 
    header ('Cache-control: no-cache, must-revalidate'); 
    header ('Pragma: private'); 

    $size = filesize($file); 
    if(isset($_SERVER['HTTP_RANGE'])) 
    { 
        list($a, $range)=explode("=",$_SERVER['HTTP_RANGE']); 
        //if yes, download missing part 
        str_replace($range, "-", $range); 
        $size2=$size-1; 
        $new_length=$size2-$range; 
        header("HTTP/1.1 206 Partial Content"); 
        header("Content-Length: $new_length"); 
        header("Content-Range: bytes $range$size2/$size"); 
    } 
    else 
    { 
        $size2=$size-1; 
        header("Content-Range: bytes 0-$size2/$size"); 
        header("Content-Length: ".$size); 
    } 
    $status = NULL;
    if ($file = fopen($file, 'rb')) 
    { 
        while(!feof($file) and (connection_status()==0)) 
        { 
            print(fread($file, 1024*8)); 
            flush(); 
        } 
        $status = (connection_status()==0); 
        fclose($file); 
    } 
    return($status); 
} 

if ($_GET['socialReportDownload']){
	generateSocialReport();
	dwfile('/var/www/JMJ_test/socialReport.csv');
}

if($_GET['withinGameReportDownload']){
	generateWithinGameReport();
	dwfile('withinGameReport.csv');
}

if($_GET['gameReportDownload']){
	generateReport();
	dwfile('report.csv');
}

if($_GET['paymentReportDownload']){
	generatePaymentReport();
	dwfile('paymentReport.csv');
}

if($_GET['surveyReportDownload']){
	generateSurveyReport();
	dwfile('surveyReport.csv');
}

if($_GET['vnaFormatDownload']){
	generateVnaFormat();
	dwfile('network.vna');
}
?>